home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2559 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  43 lines

  1. Path: news.onramp.net!usenet
  2. From: purple@haze.net
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Beginner Question : Filling Arrays from Text Files
  5. Date: Mon, 22 Jan 1996 08:59:14 GMT
  6. Organization: On-Ramp; Individual Internet Connections; Dallas/Ft Worth/Houston, TX USA
  7. Message-ID: <4dvdo1$kg6@news.onramp.net>
  8. References: <1f7cc$13361c.91@news.hampshire.edu>
  9. NNTP-Posting-Host: stemmons15.onramp.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. <sbfF94@hamp.hampshire.edu> wrote:
  13.  
  14. >Hi, we're writing a cgi program in c to produce a multiple-choice quiz on a web page.  The program currently works, but we need to convert the contents of the question and answer arrays from being defined within the code:
  15.  
  16. >char *question[] =
  17. >{"ANT","BAT","CAT","DOG","EAR","FUN","GOO","HIPPO","ID","JON"};
  18.  
  19. >to being filled by an external text file with an indeterminate (and changable) number of entries.  We are trying to figure out how to use fread() to do this, but are totally confused as to how.  We need to know : a)the required format of the text file to make the entire file readable into the array  b) how to get the array "question" to fill itself with as many pointers/strings as are in the text file and c)use the number of entries in the text file to constrain rand().
  20. >        Also, how does sizeof() work with fread, and do we need to use it?
  21. >we are compiling on unix using gcc.
  22. >                                                                                                                                                                                            Thanks,
  23. >                                                                                                                                                                                                        Shana Furman
  24. >    
  25.                                                                                                                                                                                                      Jon Seag
  26.  
  27. I would use fgets to find out how many line are in the text file(then
  28. rewind()).
  29. char **question;
  30. qusetion = (char**)malloc(num_lines);
  31. qusetion[0] = (char*)malloc(strlen(str)+1); 
  32. or *(question+index) = (char*)malloc(strlen(str)+1);
  33. .
  34. .
  35. Use a dynamic array for question allocating sterlen + 1 for every line
  36. read out of the file(using fgets again) into a buffer(str) as big as
  37. the biggest possable entry. Then just copy the buffer into the pointer
  38. and use the buffer again.
  39. Bruce Mack rwebb@oramp.net Dallas,TX
  40.  
  41.  
  42.  
  43.